import Vue from "vue";
import VueRouter from "vue-router";
import store from "../store"

//使用VueRouter
Vue.use(VueRouter);

var routes=[
    // {
    //     //路由路径
    //     path:"/",
    //     //路由名字
    //     name:"Manage",
    //     redirect:"/main",
    //     //跳转组件
    //     component:()=>import('@/views/Manage.vue'),
    //     children:[
    //         // {
    //         //     path:"user",
    //         //     name:"UserManager",
    //         //     meta:"系统管理/用户管理",
    //         //     component:()=>import('@/views/UserManager.vue')
    //         // },
    //         // {
    //         //     path:"role",
    //         //     name:"RoleManager",
    //         //     meta:"系统管理/角色管理",
    //         //     component:()=>import('@/views/RoleManager.vue')
    //         // },
    //         // {
    //         //     path:"menu",
    //         //     name:"MenuManager",
    //         //     meta:"系统管理/菜单管理",
    //         //     component:()=>import('@/views/MenuManager.vue')
    //         // },
    //         // {
    //         //     path:"main",
    //         //     name:"main",
    //         //     meta:"主页",
    //         //     component:()=>import('@/views/Main.vue')
    //         // },
    //         // {
    //         //     path:"profile",
    //         //     name:"profile",
    //         //     meta:"个人信息",
    //         //     component:()=>import('@/views/Profile.vue')
    //         // },
    //         // {
    //         //     path:"fileManager",
    //         //     name:"fileManager",
    //         //     meta:"系统管理/文件管理",
    //         //     component:()=>import('@/views/FileManager.vue')
    //         // }
    //         ]
    // },
    {
        path: "/login",
        name: "Login",
        component: ()=>import('@/views/Login.vue'),
    },
    {
        path: "/404",
        name: "404",
        component: ()=>import('@/components/common/NotFound_404.vue'),
    },
];

export const resetRoute=()=>{
    router.matcher=new VueRouter({
            mode:"history",
            routes:routes,
        }
    );
};

export const  updateDate= ()=>{
    let names = router.getRoutes().map(val=>val.name);
    if (!names.includes('Manage')&&localStorage.getItem("user")!=null){
        let menus =  JSON.parse(localStorage.getItem("user")).menus;
        let children=[];
        menus.forEach(menu =>{
            if (menu.parentId!=null||(menu.path!='' &&menu.path!=null)){
                let child={};
                child.path=menu.path.substr(1,menu.path.length-1);
                child.name=child.path;
                child.meta=menu.description;
                child.component=()=>import('@/'+menu.pagePath+'.vue');
                children.push(child)
            }
        });
        let child ={
            path:"profile",
            name:"profile",
            meta:"个人信息",
            component:()=>import('@/views/Profile.vue')
        };
        children.push(child);
        let route=    {
            //路由路径
            path:"/",
            //路由名字
            name:"Manage",
            redirect:"/main",
            //跳转组件
            component:()=>import('@/views/Manage.vue'),
        }
        route.children=children;
        router.addRoute(route);
    }

}

const router=new VueRouter({
        mode:"history",
        routes:routes,
    }
);

updateDate();

router.beforeEach((to,from,next)=>{
    if (to.meta!=null && !(to.meta==='')){
        localStorage.setItem("currentAddress",to.meta);
        store.commit("setPath");    //触发store的数据更新，重新渲染
    }
    if (to.matched.length==0){
        if (localStorage.getItem("user")==null || localStorage.getItem("user")===""){
            next("/login");
        }else{
            next("/404")
        }
    }
    next();
})

// router.afterEach((to,from)=>{
//     updateDate();
// });


//配置导出路由
export default router;
